package com.how2java.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.how2java.bean.Category;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.how2java.bean.Product;

@Namespace("/")
@ParentPackage("struts-default")
@Results({@Result(name="show", location="/show.jsp"),
@Result(name="home", location="/index.jsp")})
public class ProductAction{
	
	 public ProductAction(){
	        System.out.println("product action instance: " + this);
	    }
	 
	 private String name;
     
	    public String getName() {
	        return name;
	    }
	 
	    public void setName(String name) {
	        this.name = name;
	    }
	 
	    
	 private Date date;
     
	    public Date getDate() {
	        return date;
	    }
	 
	    public void setDate(Date date) {
	        this.date = date;
	    }
	    
//	    public void validate(){
//	        if ( product.getName().length() == 0 ){
//	            addFieldError( "product.name", "name can't be empty" );
//	        }
//	    }  
	    
    private Product product;
     
    private List<Category> categories =new ArrayList();
     
    public List<Category> getCategories() {
        return categories;
    }
 
    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
    @Action("showProduct")
    public String show() {
        product = new Product();
        product.setName("iphone7");
        return "show";
    }
  
    public String add() {
        return "show";
    }
     
    public String list() {
        Category category1 =new Category();
        category1.setId(1);
        category1.setName("category1");
         
        Category category2 =new Category();
        category2.setId(2);
        category2.setName("category2");
         
        categories.add(category1);
        categories.add(category2);
         
        List<Product> products1=new ArrayList();
        Product p1 = new Product();
        p1.setId(1);
        p1.setName("product1");
        Product p2 = new Product();
        p2.setId(2);
        p2.setName("product2");
        Product p3 = new Product();
        p3.setId(3);
        p3.setName("product3");
  
        products1.add(p1);
        products1.add(p2);
        products1.add(p3);
         
        List<Product> products2=new ArrayList();
        Product p4 = new Product();
        p4.setId(4);
        p4.setName("product4");
        Product p5 = new Product();
        p5.setId(5);
        p5.setName("product5");
        Product p6 = new Product();
        p6.setId(6);
        p6.setName("product6");
         
        products2.add(p4);
        products2.add(p5);
        products2.add(p6);
         
        category1.setProducts(products1);
        category2.setProducts(products2);
         
        return "list";
  
    }
    public String addPage(){
        name = "default name";
        return "addPage";
    }
  
    public Product getProduct() {
        return product;
    }
  
    public void setProduct(Product product) {
        this.product = product;
    }
}